package com.story.api.moduler.song.controller;

import com.story.api.common.exception.BizExceptionEnum;
import com.story.api.common.exception.BussinessException;
import com.story.api.common.persistence.model.AgeType;
import com.story.api.common.result.PageResult;
import com.story.api.common.result.ServerResponse;
import com.story.api.moduler.song.aop.PlayCount;
import com.story.api.moduler.song.aop.UseApp;
import com.story.api.moduler.song.service.SongService;
import com.story.api.moduler.song.service.SongV2Service;
import com.story.api.moduler.song.transfer.BannerVo;
import com.story.api.moduler.song.transfer.vo.AlbumVo;
import com.story.api.moduler.song.transfer.vo.BannerAndAlbumVo;
import com.story.api.moduler.song.transfer.vo.recommend.RecommendVo;
import com.story.api.moduler.upyun.service.UpyunService;
import com.story.core.util.UriUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 版本2视频和歌曲管理控制器
 * @author yangbo
 * @date 2019-07-09 8:30
 */
@RestController
@RequestMapping(value = "/child/v2")
public class SongV2Controller {

    private static final Logger log = LoggerFactory.getLogger(SongV2Controller.class);

    @Autowired
    private SongV2Service songV2Service;

    @Autowired
    private UpyunService upyunService;

    @Autowired
    private SongService songService;

    /**
     * 年龄段数据
     * @return
     */
    @UseApp
    @GetMapping(value = "/index/ages")
    public ServerResponse<List<AgeType>> ageTypeList() {
        return ServerResponse.getSuccess("success",songV2Service.getAges());
    }

    /**
     * 首页栏目
     * @return
     */
    @GetMapping(value = "/index/banners")
    public ServerResponse<List<BannerVo>> bannerList() {
        //type = 1表示为视频栏目
        return ServerResponse.getSuccess("success",songV2Service.bannerList(1));
    }

    /**
     * 首页推荐
     * @return
     */
    @GetMapping(value = "/index/recommends")
    public ServerResponse<List<RecommendVo>> recommendList() {
        return ServerResponse.getSuccess("success",songV2Service.getRecommendList(1));
    }

    /**
     * 首页视频列表
     * @param ageRange
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/index/videos")
    public ServerResponse<PageResult> mediaPageList(@RequestParam(value = "ageRange",required = false) Integer ageRange,
                                                    @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize) {
        pageNo = pageNo < 1 ? 1: pageNo;
        pageSize = pageSize < 1 ? 6 : pageSize;
        return ServerResponse.getSuccess("success",songV2Service.getMediaPageList(ageRange,pageNo,pageSize));
    }


    /**
     * 精选部分
     * @param tab
     * @return
     */
    @GetMapping(value = "/selection")
    public ServerResponse<Map<String,Object>> selectionBannerList(@RequestParam(value = "tab") Integer tab) {
        if(tab != 0 && tab != 1) {
            throw new BussinessException(BizExceptionEnum.PARAM_ERROR);
        }
        Map<String,Object> result = new HashMap<>(8);
        //banner部分
        result.put("banner",songV2Service.bannerList(tab));
        //推荐部分
        List<RecommendVo> recommendVos = songV2Service.getRecommendList(tab);
        recommendVos = recommendVos.stream().limit(3).collect(Collectors.toList());
        result.put("recommend",recommendVos);
        //专辑推荐部分
        List<AlbumVo> albumVoList = songV2Service.recommendAlbum(tab);
        result.put("albumRec",albumVoList);
        //栏目+专辑部分
        List<BannerAndAlbumVo> bannerAndAlbumVoList = songV2Service.bannerAndAlbumList(tab);
        result.put("bannerAndAlbum",bannerAndAlbumVoList);
        return ServerResponse.getSuccess("success",result);
    }

    /**
     * 栏目下的专辑分页列表
     * @param bannerId
     * @return
     */
    @GetMapping(value = "/banner/albums")
    public ServerResponse<PageResult> albumListInBanner(@RequestParam(value = "bannerId") Integer bannerId,
                                                        @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                                        @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize) {

        pageNo = pageNo < 1 ? 1: pageNo;
        pageSize = pageSize < 1 ? 6 : pageSize;
        return ServerResponse.getSuccess("success",songV2Service.albumList(bannerId,pageNo,pageSize));
    }

    /**
     * 得到专辑的详情信息
     * @param albumId
     * @return
     */
    @GetMapping(value = "/album/detail")
    public ServerResponse<AlbumVo> albumDetail(@RequestParam(value = "albumId") Integer albumId) {
        return ServerResponse.getSuccess("success",songV2Service.albumDetail(albumId));
    }

    /**
     * 获取一个专辑下的媒体资源分页列表
     * @param albumId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/album/medias")
    public ServerResponse<PageResult> mediaResourcePageList(@RequestParam(value = "albumId") Integer albumId,
                                                            @RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                                            @RequestParam(value = "pageSize",required = false,defaultValue = "6") Integer pageSize) {
        pageNo = pageNo < 1 ? 1: pageNo;
        pageSize = pageSize < 1 ? 6 : pageSize;
        return ServerResponse.getSuccess("success",songV2Service.mediaList(albumId,pageNo,pageSize));
    }

    /**
     * 寻找相似专辑
     * @param albumId
     * @return
     */
    @GetMapping(value = "/album/similar")
    public ServerResponse<List<AlbumVo>> findSimilarAlbum(@RequestParam(value = "albumId") Integer albumId) {
        return ServerResponse.getSuccess("success",songV2Service.similarList(albumId));
    }

    /**
     * 得到播放地址
     * @param playPath 未加授权token的地址
     * @return
     */
    @PlayCount
    @GetMapping(value = "/play/address")
    public ServerResponse getPlayAddress(@RequestParam(value = "playPath") String playPath) {
        playPath = playPath + "?_upt=" + upyunService.getTokenSign( UriUtil.getUri(playPath),36000L);
        Map<String,Object> result = new HashMap<>(2);
        result.put("playAddress",playPath);
        return ServerResponse.getSuccess("success",result);
    }

    /**
     * 热门搜索前10
     * @return
     */
    @GetMapping(value = "/search/hot")
    public ServerResponse hotSearch() {
        return songService.getHotVedios(null);
    }

    /**
     * 搜索
     * @param keyword 关键字
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/search")
    public ServerResponse getSearchPageList(@RequestParam(value = "keyword",required = false) String keyword,
                                            @RequestParam(value = "pageNo",required = false) Integer currentPage,
                                            @RequestParam(value = "pageSize",required = false) Integer pageSize) {
        if(StringUtils.isBlank(keyword)) {
            return ServerResponse.getErrorMessage("请输入关键词");
        }
        keyword = keyword.trim();
        pageSize = pageSize == null ? 6 : pageSize;
        currentPage = currentPage == null ? 1 : currentPage;
        return songService.getSearchPage(currentPage,pageSize,keyword,null);
    }
}
